<template>
    <!-- 粒子效果 -->
    <vue-particles id="tsparticles" class="login_particles" :particlesInit="particlesInit"
        :particlesLoaded="particlesLoaded" :options="options" />
    <!-- 登录表单 -->
    <div class="formContainer">
        <h3>门户网站管理系统</h3>
        <el-form ref="loginFormRef" :model="loginForm" status-icon :rules="loginRules" label-width="80px" class="loginform">
            <el-form-item label="用户名" prop="username">
                <el-input class="w-90" v-model="loginForm.username" autocomplete="off" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input class="w-90" v-model="loginForm.password" type="password" autocomplete="off" />
            </el-form-item>
            <el-form-item>
                <el-button class="w-90" type="primary" @click="submitForm">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>


<script setup>
import { ref, reactive } from "vue";
import { loadFull } from "tsparticles";
import { useRouter } from "vue-router";
import axios from "axios";
import { ElMessage } from 'element-plus'
import { useStore } from "vuex";
const store = useStore()
const loginForm = reactive({
    username: 'admin',
    password: 'admin'
})

// 表单引用的响应式对象
const loginFormRef = ref()
// 表单规则
const loginRules = reactive({
    // trigger 验证逻辑触发的方式blur
    username: [
        {
            required: true, message: '请输入用户名', trigger: 'blur'
        }
    ],
    password: [
        {
            required: true, message: '请输入密码', trigger: 'blur'
        }
    ]
})
const router = useRouter()
// 提交表单的函数
const submitForm = () => {
    // 手动触发校验
    loginFormRef.value.validate((valid) => {
        // console.log(valid);
        if (valid) {
            // console.log(loginForm);
            axios.post("/adminapi/user/login", loginForm).then(res => {
                // console.log(res.data);
                if (res.data.ActionType === 'OK') {
                    // console.log(res.data.data);
                    store.commit("changeUserInfo", res.data.data)
                    // 每次登录之后都会重新加载路由
                    store.commit("changeGetterRouter", false)
                    router.push('/index')
                } else {
                    ElMessage.error(res.data.error)
                }
            })

        }
    })
}
/* const handleLogin = () => {
    localStorage.setItem('token', "13265988346")
} */

// #region 粒子效果的方法以及配置
const particlesInit = async engine => {
    await loadFull(engine);
};
const particlesLoaded = async container => {
    // console.log("Particles container loaded", container);
};

// 背景粒子效果配置
const options = {
    /* background: {
        color: {
            value: '#2b7b78'
        }
    }, */
    background: {
        size: 'cover',
        repeat: 'no-repeat',
        image: "url('/bg.jpg')"

    },
    fpsLimit: 120,
    interactivity: {
        events: {
            onClick: {
                enable: true,
                mode: 'push'
            },
            onHover: {
                enable: true,
                mode: 'repulse'
            },
            resize: true
        },
        modes: {
            bubble: {
                distance: 400,
                duration: 2,
                opacity: 0.8,
                size: 40
            },
            push: {
                quantity: 4
            },
            repulse: {
                distance: 200,
                duration: 0.4
            }
        }
    },
    particles: {
        color: {
            value: '#ffffff'
        },
        links: {
            color: '#ffffff',
            distance: 150,
            enable: true,
            opacity: 0.5,
            width: 1
        },
        collisions: {
            enable: true
        },
        move: {
            direction: 'none',
            enable: true,
            outMode: 'bounce',
            random: false,
            speed: 2,
            straight: false
        },
        number: {
            density: {
                enable: true,
                area: 800
            },
            value: 80
        },
        opacity: {
            value: 0.5
        },
        shape: {
            type: 'circle'
        },
        size: {
            random: true,
            value: 5
        }
    },
    detectRetina: true
}
// #endregion

</script>


<style lang="scss" scoped>
.formContainer {
    width: 500px;
    height: 300px;
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background-color: rgba($color: #000000, $alpha: 0.3);
    color: white;
    text-align: center;
    padding: 20px;

    h3 {
        font-size: 30px;
    }

    .loginform {
        margin-top: 30px;

        .w-90 {
            width: 90%;
        }
    }


}

:deep(.el-form-item__label) {
    color: white;
}
</style>